// #include "../src/windows.hpp"
#include "../src/common.h"
#include "../src/image.hpp"
// #include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <chrono>

struct Point {
	double x, y;
};

	Point* R, * Q;
	Point P0;
Point decas (Point P[], int n, double t) {

	int m, i;

	for (i = 0; i <= n; i++) {
		R [i] = P [i];
	}

	for (m = n; m > 0; m--) {
		for (i = 0; i <= m-1; i++) {
			Q [i].x = R [i].x + t*(R [i+1].x - R [i].x);
			Q [i].y = R [i].y + t*(R [i+1].y - R [i].y);
		}

		for (i = 0; i < m - 1; i++) {
			R [i] = Q [i];
		}
	}

	// P0 = R [0];
	// delete R;
	// delete Q;

	return R [0];


}
WINDOW window1 (1000, 1000);
IMAGE ts ("./imagetest.jpg", window1);

void bez_to_Points (Point P[], int n, Point pts [], int nPoints) {
	double t = 0.0, delt = 1.0 / nPoints;
	Point p;
	R = new Point [n + 2];
	Q = new Point [n + 2];
	for (int i = 0; i <= nPoints; i++) {
		pts [i] = decas (P, n, t);
		t += delt;
		// std::cerr << pts [i].x << "  " << pts [i].y << std::endl;
	}
}

int main () {

	Point p [5] = {{2,4},{3,1},{5,6},{7,3}};
	int ptss = 100;
	Point pts [ptss + 3];
	bez_to_Points (p, 4, pts, ptss);

	for (int i = 0 ; i < ptss; i ++) {
		ts.drawPixel (pts[i].x * 100, pts[i].y * 100);
		ts.drawLineAA (pts[i].x * 100, pts[i].y * 100, pts[i+1].x * 100, pts[i+1].y * 100);
	}
	window1.update ();
	// window1.sho
	getchar ();
	SDL_Quit ();
	return 0;

}